home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10726 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: news.bellglobal.com!news
  2. From: Mathieu Frenette <midk@interlinx.qc.ca>
  3. Newsgroups: comp.lang.c++
  4. Subject: Q: Template functions with constants instead of types????
  5. Date: Tue, 05 Mar 1996 22:02:51 -0500
  6. Organization: Antares Productions
  7. Message-ID: <313D005B.1E1D@interlinx.qc.ca>
  8. NNTP-Posting-Host: 204.101.120.37
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  13.  
  14. First, let's make sure I understand what "template" exactly is...  I 
  15. know one can define template functions/classes using general types and 
  16. then use these functions/classes while specifying the needed type and 
  17. the compiler generates all the different cases, one function per type 
  18. used in the program.
  19.  
  20. Ok, now, let's go with the rough stuff! :)  What I want is to be able to 
  21. define a function using a DEFINEd constant (let's name it "CONSTANT") 
  22. and then be able to call the function with CONSTANT having arbitrary 
  23. values.  For each value of CONSTANT, the compiler would generate a 
  24. different function.
  25.  
  26. In example, let's suppose we could define something like :
  27.  
  28.   template <constant T> inline void Func( void )
  29.   {
  30.     printf( "%d\n", T );
  31.   }
  32.  
  33. And then use it like that :
  34.  
  35.   Func<1>();
  36.   Func<2>();
  37.   Func<3>();
  38.  
  39. The compiler would generate three similar Func functions with T replaced 
  40. by the values 1,2,3.
  41.  
  42. If you know the syntax for such a template, PLEASE LET ME KNOW!!! :)
  43.  
  44. If you're wondering why I'm looking for such a complicated thing, here's 
  45. why :
  46.  
  47. I have a function that uses a DEFINEd constant (which it absolutely 
  48. needs for optimization reasons) and I want to include this function in a 
  49. library for all my demo group other coders to use.  The problem is that, 
  50. if the function is used 4 times in the program, it'll be with 4 
  51. different values assigned to the constant...
  52.  
  53. If you have any idea else than the "hypothetical" template <const T>, 
  54. please let me know...
  55.  
  56. Thanks in advance...
  57. -- 
  58. Kind Regards,
  59.     Mathieu.
  60. --------------------------------------------------------
  61. Mathieu Frenette,                     aka ]\/[id ]<night
  62. Antares Prod.            
  63. <midk@interlinx.qc.ca>       Don't let the Bit Bug Byte!
  64. --------------------------------------------------------
  65.  
  66.  
  67.